﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace VNSoft.Administrator
{
    public partial class BaiViet : System.Web.UI.Page
    {
        clsXuLy ex = new clsXuLy();
        string date = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancelBaiViet_Click(sender, e);
            }
        }
        protected void btnAddBaiViet_Click(object sender, EventArgs e)
        {
            if (txtTieuDe.Text.Trim().Equals(""))
                gstGetMess("Nhập tiêu đề bài viết", "");
            else
                if (fu.HasFile)
                {
                    try
                    {
                        date = DateTime.Now.ToString("ddMMyyyhhmmss");
                        SqlParameter[] p;
                        p = new SqlParameter[9];
                        p[0] = new SqlParameter("@TieuDe", txtTieuDe.Text.Trim());
                        p[0].SqlDbType = SqlDbType.NVarChar;
                        p[1] = new SqlParameter("@TomTat", txtTomTat.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;
                        p[2] = new SqlParameter("@NoiDung", fcknd.Value);
                        p[2].SqlDbType = SqlDbType.NVarChar;
                        p[3] = new SqlParameter("@HinhAnh", "Administrator/ImgBaiViet/" + date  + fu.FileName.Substring(fu.FileName.LastIndexOf('.')));
                        p[3].SqlDbType = SqlDbType.NVarChar;
                        p[4] = new SqlParameter("@HienThi", ckHienthi.Checked ? 1 : 0);
                        p[4].SqlDbType = SqlDbType.Int;
                        p[5] = new SqlParameter("@ID", Guid.NewGuid());
                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[6] = new SqlParameter("@Date", DateTime.Now.ToShortDateString());
                        p[6].SqlDbType = SqlDbType.SmallDateTime;
                        p[7] = new SqlParameter("@Type", int.Parse(dlLoaiBaiViet.SelectedValue));
                        p[7].SqlDbType = SqlDbType.Int;
                        p[8] = new SqlParameter("@SEO", txtTuKhoaSEO.Text.Trim());
                        p[8].SqlDbType = SqlDbType.NVarChar;

                        ex.ExecuteNonQuerry("sp_BaiViet_Add", p);
                        fu.SaveAs(Server.MapPath("../Administrator/ImgBaiViet/" + date  + fu.FileName.Substring(fu.FileName.LastIndexOf('.'))));
                        btnCancelBaiViet_Click(sender, e);
                        gstGetMess("Thêm thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess(ax.Message, "");
                    }
                }
                else gstGetMess("Chọn hình ảnh Upload cho bài viết", "");
        }
        private void gstGetMess(string gstMess, string gstLink)
        {
            if (gstLink == "")
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
            else
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

        }
        protected void btnUpdateBaiViet_Click(object sender, EventArgs e)
        {
            
            if (txtTieuDe.Text.Trim().Equals(""))
                gstGetMess("Nhập tiêu đề bài viết", "");
            else
            {
                try
                {
                    SqlParameter[] p;
                    Guid ID = new Guid(hdMaBaiViet.Value);
                    if (!fu.HasFile)
                    {
                        p = new SqlParameter[10];
                        p[0] = new SqlParameter("@TieuDe", txtTieuDe.Text.Trim());
                        p[0].SqlDbType = SqlDbType.NVarChar;
                        p[1] = new SqlParameter("@TomTat", txtTomTat.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;
                        p[2] = new SqlParameter("@NoiDung", fcknd.Value);
                        p[2].SqlDbType = SqlDbType.NVarChar;
                        p[3] = new SqlParameter("@ID", ID);
                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[4] = new SqlParameter("@HienThi", ckHienthi.Checked ? 1 : 0);
                        p[4].SqlDbType = SqlDbType.Int;
                        p[5] = new SqlParameter("@HinhAnh","" );
                        p[5].SqlDbType = SqlDbType.NVarChar;
                        p[6] = new SqlParameter("@Date", DateTime.Now.ToShortDateString());
                        p[6].SqlDbType = SqlDbType.SmallDateTime;
                        p[7] = new SqlParameter("@Index",2);
                        p[7].SqlDbType = SqlDbType.Int;
                        p[8] = new SqlParameter("@Type", int.Parse(dlLoaiBaiViet.SelectedValue));
                        p[8].SqlDbType = SqlDbType.Int;
                        p[9] = new SqlParameter("@SEO", txtTuKhoaSEO.Text.Trim());
                        p[9].SqlDbType = SqlDbType.NVarChar;

                        ex.ExecuteNonQuerry("sp_BaiViet_Update", p);
                        btnCancelBaiViet_Click(sender, e);
                        gstGetMess("Cập nhật thành công", "");
                    }
                    else
                    {
                        date = DateTime.Now.ToString("ddMMyyyhhmmss");
                        p = new SqlParameter[10];
                        p[0] = new SqlParameter("@TieuDe", txtTieuDe.Text.Trim());
                        p[0].SqlDbType = SqlDbType.NVarChar;
                        p[1] = new SqlParameter("@TomTat", txtTomTat.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;
                        p[2] = new SqlParameter("@NoiDung", fcknd.Value);
                        p[2].SqlDbType = SqlDbType.NVarChar;
                        p[3] = new SqlParameter("@HinhAnh", "Administrator/ImgBaiViet/" + date + fu.FileName.Substring(fu.FileName.LastIndexOf('.')));
                        p[3].SqlDbType = SqlDbType.NVarChar;
                        p[4] = new SqlParameter("@HienThi", ckHienthi.Checked ? 1 : 0);
                        p[4].SqlDbType = SqlDbType.Int;
                        p[5] = new SqlParameter("@ID", ID);
                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[6] = new SqlParameter("@Date", DateTime.Now.ToShortDateString());
                        p[6].SqlDbType = SqlDbType.SmallDateTime;
                        p[7] = new SqlParameter("@Index", 1);
                        p[7].SqlDbType = SqlDbType.Int;
                        p[8] = new SqlParameter("@Type", int.Parse(dlLoaiBaiViet.SelectedValue));
                        p[8].SqlDbType = SqlDbType.Int;
                        p[9] = new SqlParameter("@SEO", txtTuKhoaSEO.Text.Trim());
                        p[9].SqlDbType = SqlDbType.NVarChar;

                        ex.ExecuteNonQuerry("sp_BaiViet_Update", p);
                        if (!hdMaHinhAnh.Value.Equals(""))
                        {
                            FileInfo f = new FileInfo(Server.MapPath("../" + hdMaHinhAnh.Value));
                            if (f.Exists)
                                f.Delete();
                        }
                        fu.SaveAs(Server.MapPath("../Administrator/ImgBaiViet/" + date + "_" + ex.ConvertText(fu.FileName)));

                        btnCancelBaiViet_Click(sender, e);
                        gstGetMess("Cập nhật thành công", "");
                    }
                }
                catch (Exception ax)
                {
                    gstGetMess(ax.Message, "");
                }
            }
        }
        protected void btnCancelBaiViet_Click(object sender, EventArgs e)
        {
            try
            {
                gvBaiViet.DataSource = ex.GetData("sp_BaiViet_List");
                gvBaiViet.DataBind();
                btnUpdateBaiViet.Visible = false;
                btnAddBaiViet.Visible = true;
                txtTieuDe.Text = "";
                txtTomTat.Text = "";
                txtTuKhoaSEO.Text = "";
                fcknd.Value = "";
                ckHienthi.Checked = true;
                dlLoaiBaiViet.SelectedIndex = 0;
            }
            catch (Exception ax)
            {

            }
        }
        protected void gvBaiViet_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();
            hdMaBaiViet.Value = ID;
            Guid gID = new Guid(ID);
            SqlParameter[] p;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@ID", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            if (e.CommandName.Equals("Select"))
            {
                try
                {
                    DataTable dt = ex.GetData("sp_BaiViet_ListByID", p);
                    if (dt.Rows.Count > 0)
                    {
                        txtTieuDe.Text = dt.Rows[0]["Title"].ToString();
                        txtTomTat.Text = dt.Rows[0]["Description"].ToString();
                        dlLoaiBaiViet.SelectedValue = dt.Rows[0]["Type"].ToString();
                        txtTuKhoaSEO.Text = dt.Rows[0]["KeySEO"].ToString();
                        fcknd.Value = dt.Rows[0]["Body"].ToString();
                        ckHienthi.Checked = dt.Rows[0]["Active"].ToString().Equals("1") ? true : false;
                        hdMaHinhAnh.Value = dt.Rows[0]["Image"].ToString();

                    }
                    btnUpdateBaiViet.Visible = true;
                    btnAddBaiViet.Visible = false;
                }
                catch (Exception ax)
                {

                }
            }
            else
                if (e.CommandName.Equals("Del"))
                {
                    try
                    {
                        SqlParameter[] p1;
                        p1 = new SqlParameter[1];
                        p1[0] = new SqlParameter("@ID", gID);
                        p1[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        string hinhanh = ex.GetValueString_Text("select Image from tblarticle where articleid='" + gID+"'");
                        ex.ExecuteNonQuerry("sp_BaiViet_Del", p1);

                        if (hinhanh.Equals("") || hinhanh.Equals(DBNull.Value))
                        { }
                        else
                        {
                            FileInfo f = new FileInfo(Server.MapPath("../" + hinhanh));
                            if (f.Exists)
                                f.Delete();
                        }
                        btnCancelBaiViet_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {

                        gstGetMess("Lỗi: " + ax.Message, "");
                    }
                }
        }
    }
}